package com.example.hello.views.assignments;

import com.example.hello.*;
import com.example.hello.POJO.AssignmentPOJO;
import com.example.hello.POJO.OrganizationPOJO;
import com.example.hello.POJO.TrackPOJO;
import com.example.hello.views.organizations.Organizations;
import com.example.hello.views.tracks.Tracks;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.VerticalLayout;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Set;

public class EditAssignment extends VerticalLayout implements View {

    public static final String NAME = "edit_assignment";

    private final TextField title = new TextField("Assignment Name");
    private final TextArea description = new TextArea("Description");
    private final ListBox orgList = new ListBox("Organization");
    private final ListBox trackList = new ListBox("Track");
    private final TextField submissionPoints = new TextField("Submission Points");
    private final TextField reviewPoints = new TextField("Review Points");

    private AssignmentPOJO assignment;

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        String[] parameters = event.getParameters().split("/");
        if (!parameters[0].equals("")) assignment = Assignment.GETAssignment(parameters[0]);

        if (assignment != null) {
            Set<OrganizationPOJO> tmpSet = Organizations.GETOrganizations("?track=" + assignment.getTrack());
            OrganizationPOJO assignmentOrg = tmpSet.iterator().next();
            title.setValue(assignment.getName());
            description.setValue(assignment.getDescription());
            orgList.setSelectedValue(assignmentOrg.getDecodedName());

            Set<TrackPOJO> tracks = Tracks.GETTracks("?org=" + assignmentOrg.getEncodedName());
            for (TrackPOJO track : tracks) trackList.addItem(track.getDecodedName());
            for (String item : trackList.getItems()) System.out.println(item);
            try {
                trackList.setSelectedValue(URLDecoder.decode(assignment.getTrack(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            submissionPoints.setValue(Integer.toString(assignment.getSubmissionPoints()));
            reviewPoints.setValue(Integer.toString(assignment.getReviewPoints()));
        }
    }

    public EditAssignment() {
        setWidth("500px");

        initAssignment();
    }

    public void initAssignment() {
        title.setWidth("60%");
        description.setHeight("300px");
        description.setWidth("100%");
        submissionPoints.setWidth("50px");
        reviewPoints.setWidth("50px");

        orgList.addItem("-- select organization --");
        trackList.addItem("-- select track --");

        // populate organization list
        Set<OrganizationPOJO> orgs = Organizations.GETOrganizations("");
        for (OrganizationPOJO org : orgs) orgList.addItem(org.getDecodedName());
        final StringWrap selectedOrg = new StringWrap();
        orgList.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                if (event instanceof ListBoxChangeEvent) {
                    selectedOrg.setString(((ListBoxChangeEvent) event).getSelected());
                    trackList.removeAllItems();
                    trackList.addItem("-- select track --");
                    if (selectedOrg.toString().equals("-- select organization --"))
                        return;

                    // populate track list
                    Set<TrackPOJO> tracks = Tracks.GETTracks("?org=" + selectedOrg);
                    for (TrackPOJO track : tracks) trackList.addItem(track.getDecodedName());
                }
            }
        });

        final StringWrap selectedTrack = new StringWrap();
        trackList.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                if (event instanceof ListBoxChangeEvent)
                    selectedTrack.setString(((ListBoxChangeEvent) event).getSelected());
            }
        });

        BootstrapButton back = new BootstrapButton("Back");
        BootstrapButton submitAssignment = new BootstrapButton("Submit");
        back.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                getUI().getNavigator().navigateTo(Assignments.NAME + "/" + assignment.getEncodedName());
            }
        });
        submitAssignment.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                AssignmentPOJO assignment = new AssignmentPOJO();
                assignment.setName(title.getValue());
                assignment.setDescription(description.getValue());
                assignment.setTrack((String) trackList.getData());
                assignment.setSubmissionPoints(Integer.valueOf(submissionPoints.getValue()));
                assignment.setReviewPoints(Integer.valueOf(reviewPoints.getValue()));

                Assignments.addAssignment(assignment);
                getUI().getNavigator().navigateTo(Assignments.NAME + "/" + assignment.getEncodedName());
                Notification.show("Assignment \"" + assignment.getName() + "\" was successfully created!");
            }
        });

        addComponents(
                title,
                new VSpacer(20),
                description,
                new VSpacer(20),
                orgList,
                new VSpacer(20),
                trackList,
                new VSpacer(20),
                new HorizontalLayout(submissionPoints, new HSpacer(20), reviewPoints),
                new VSpacer(20),
                new HorizontalLayout(back, new HSpacer(20), submitAssignment)
        );
    }

}